package com.sfzd5.fasttextknowledge;

import com.github.jfasttext.JFastText;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.huaban.analysis.jieba.JiebaSegmenter;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FSHelper {
    HashSet<String> stopwords;
    JFastText fastText;
    Map<Integer, Knowledge> ks;
    Pattern fil = Pattern.compile("[^0-9a-zA-Z\\u4e00-\\u9fa5]+");

    public List<Knowledge> GetKnowledgePath(JFastText.ProbLabel p)
    {
        String kid = p.label.substring(9);
        return GetKnowledgePath(Integer.parseInt(kid));
    }

    public String GetKnowledgeName(JFastText.ProbLabel p)
    {
        List<Knowledge> ks = GetKnowledgePath(p);
        return ks.get(ks.size()-1).n;
    }

    public List<Knowledge> GetKnowledgePath(int kid)
    {
        List<Knowledge> kls = new ArrayList<>();
        Knowledge kl = ks.get(kid);
        for (int id : kl.c){
            kls.add(ks.get(id));
        }
        kls.add(kl);
        return kls;
    }

    public String cut(String txt)
    {
        Matcher m = fil.matcher(txt);
        txt = m.replaceAll(" ").toLowerCase();
        JiebaSegmenter segmenter = new JiebaSegmenter();
        List<String> ss = segmenter.sentenceProcess(txt);
        StringBuilder sb = new StringBuilder();
        for(String s : ss){
            if(!stopwords.contains(s)){
                sb.append(s).append(' ');
            }
        }
        return sb.toString();
    }

    public FSHelper(String km)
    {
        File model = new File("mode", km + ".model");
        fastText = new JFastText();
        String modelPath = model.getAbsolutePath();
        fastText.loadModel(modelPath);

        File kspath = new File("mode", km + "_ks.json");
        try {
            String sjson = FileUtils.readFileToString(kspath, "UTF-8");
            java.lang.reflect.Type type = new TypeToken<Map<Integer, Knowledge>>(){}.getType();
            ks = new Gson().fromJson(sjson, type);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("出错科目json");
        }

        File stppath = new File("mode", "stopwords.txt");
        try {
            stopwords = new HashSet<String>(FileUtils.readLines(stppath, "UTF-8"));
            stopwords.add(" ");
            stopwords.add("\t");
            stopwords.add("\n");
            stopwords.add("\r");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("出错stopwords");
        }
    }

    /**
     * 识别知识点
     * @param html 要识别的html文本
     * @param k 返回的最多知识点数量
     * @return
     */
    public List<JFastText.ProbLabel> Predict(String html, int k)
    {
        String txt = cut(html);
        List<JFastText.ProbLabel> predictions = fastText.predictProba(txt, k);
        return predictions;
    }

}
